We are going to make some plotly plots.
library(tidyverse)
## ── Attaching packages ─────────────────────────────────────── tidyverse 1.3.2 ──
## ✔ ggplot2 3.3.6 ✔ purrr 0.3.4
## ✔ tibble 3.1.8 ✔ dplyr 1.0.10
## ✔ tidyr 1.2.1 ✔ stringr 1.4.1
## ✔ readr 2.1.2 ✔ forcats 0.5.2
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
library(p8105.datasets)
library(plotly)
##
## Attaching package: 'plotly'
##
## The following object is masked from 'package:ggplot2':
##
## last_plot
##
## The following object is masked from 'package:stats':
##
## filter
##
## The following object is masked from 'package:graphics':
##
## layout
library(dplyr)
library(readxl)
library(ggplot2)
library(patchwork)
library(lubridate)
##
## Attaching package: 'lubridate'
##
## The following objects are masked from 'package:base':
##
## date, intersect, setdiff, union
library(ggridges)
let’s get some data.
data("ny_noaa")
noaa_tidy = ny_noaa |>
janitor::clean_names() |>
separate(date, into = c("year", "month", "day"), convert = TRUE) |>
mutate(
prcp = prcp / 10 ,
tmax = as.numeric(tmax) / 10,
tmin = as.numeric(tmin) / 10,
year = as.numeric(year),
day = as.numeric(day),
month = recode_factor(month,
"01" = "January",
"02" = "February",
"03" = "March",
"04" = "April",
"05" = "May",
"06" = "June",
"07" = "July",
"08" = "August",
"09" = "September",
"10" = "October",
"11" = "November",
"12" = "December"
)) |>
relocate(year, month, day, everything())
Scatterplot
tmax_noaa = noaa_tidy |>
group_by(id, year, month) |>
summarize(mean_tmax = mean(tmax, na.rm = TRUE))
## `summarise()` has grouped output by 'id', 'year'. You can override using the
## `.groups` argument.
tmax_noaa |>
plot_ly(
x = ~year, y = ~mean_tmax, color = ~ month,
type = "scatter", mode = "markers", alpha = .5, text = "Data from the NOAA dataset"
)
## Warning: Ignoring 36342 observations
## Warning in RColorBrewer::brewer.pal(N, "Set2"): n too large, allowed maximum for palette Set2 is 8
## Returning the palette you asked for with that many colors
## Warning in RColorBrewer::brewer.pal(N, "Set2"): n too large, allowed maximum for palette Set2 is 8
## Returning the palette you asked for with that many colors
Boxplots
tmax_noaa |>
plot_ly(
y = ~mean_tmax, color = ~month,
type = "box", colors = "viridis")
## Warning: Ignoring 36342 observations
Bar plot
tmax_noaa |>
plot_ly(
x = ~month, y = ~mean_tmax,
type = "bar", colors = "viridis")
## Warning: Ignoring 36342 observations
Not here though.